﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

using MAPDavy.Entities;
using MAPDavy.BusinessLogic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace MAPDavy.WebMVC3.Controllers
{
    public class TestController : Controller
    {
        public TestController()
        {
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                container.BuildUp(this.GetType(), this);
            }
        }

        [Dependency]
        public ITestBL  TestBL { get; set; }

        //[HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult GetTestServiceJson(Int32 subjectId=0)
        {
           var tests = TestBL.GetFromTestBySubjectId(subjectId);

           var result = from t in tests
                        select new { Id = t.Id, Test = t.NameTest };

           return Json(result, JsonRequestBehavior.AllowGet);

           //return new JsonNetResult(result);
        }

        [Authorize]
        public ActionResult GetTestbyTermServiceJson(int termId=0, int gradeId=0, Int32 subjectId=0)
        {
            var tests = TestBL.GetFromTestByTermIdAndGradeId(termId, gradeId, subjectId);

            var result = from t in tests
                         select new { Id = t.Id, Test = t.NameTest };

            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [Authorize]
        public ActionResult GetTestbyTermAndGradeServiceJson(int termId = 0, int gradeId = 0)
        {
            var tests = TestBL.GetFromTestByTermIdAndGradeId(termId, gradeId);

            var result = from t in tests
                         select new { Id = t.Id, Subject = t.Subject.NameSubject };

            return Json(result, JsonRequestBehavior.AllowGet);

        }


        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Test/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Test/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Test/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
